/*
 * Copyright: Copyright (c) 2014-2015 ZhongDaMen,Inc.All Rights Reserved. 
 * Company:中大门网络科技有限公司
 */

package com.sc.util.data;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.sc.dal.model.goods.GoodsClass;
import com.sc.util.exception.ExceptionEnum;


/**
 * <h1>数据处理</h1>
 * @ClassName: DataUtil
 * @Description: 数据处理
 * @author zdm-maoyongbin
 * @date Aug 19, 2015 10:12:24 AM
 *
 */
public class DataUtil {
	
	/**
	  * <h2>处理异常反回结果</h2>
	  * @Title: handleResultMap
	  * @Description: 处理异常反回结果
	  * @param resultMap
	  * @param exceptionEnum
	  * @return void    返回类型
	 */
	public static void handleResultMap(Map<String,Object> resultMap, ExceptionEnum exceptionEnum){
		resultMap.put("resultSuccess", exceptionEnum.getSuccess());
		resultMap.put("resultCode", exceptionEnum.getCode());
		resultMap.put("resultDesc", exceptionEnum.getDesc());
	}
	
	/**
	  * <h2>处理异常反回结果</h2>
	  * @Title: handleResultMap
	  * @Description: 处理异常反回结果
	  * @param resultMap
	  * @param exceptionEnum
	  * @param dataObject
	  * @return void    返回类型
	 */
	public static void handleResultMap(Map<String,Object> resultMap, ExceptionEnum exceptionEnum,Object dataObject){
		resultMap.put("resultSuccess", exceptionEnum.getSuccess());
		resultMap.put("resultCode", exceptionEnum.getCode());
		resultMap.put("resultDesc", exceptionEnum.getDesc());
		resultMap.put("data", dataObject);
	}
	
	/**
	  * <h2>处理异常反回结果</h2>
	  * @Title: handleResultMap
	  * @Description: 处理异常反回结果
	  * @param resultMap
	  * @param exceptionEnum
	  * @param dataObject
	  * @return void    返回类型
	 */
	public static void handleResultMap(Map<String,Object> resultMap, ExceptionEnum exceptionEnum,Object countObject,Object pagesObject,Object dataObject){
		resultMap.put("resultSuccess", exceptionEnum.getSuccess());
		resultMap.put("resultCode", exceptionEnum.getCode());
		resultMap.put("resultDesc", exceptionEnum.getDesc());
		resultMap.put("count", countObject);
		resultMap.put("pages", pagesObject);
		resultMap.put("data", dataObject);
	}
	
	/**
	  * <h2>递规取得商品所有分类</h2>
	  * @Title: getGoodsCateList
	  * @Description: 递规取得所有分类
	  * @param @param goodsCateList
	  * @param @param id
	  * @return List<Object>    返回类型
	 */
	public static List<Object> getGoodsCateList(List<GoodsClass> goodsClassList, Integer parentId){
		List<Object> goodsClassMapList = new ArrayList<Object>();
		for(GoodsClass goodsClass : goodsClassList){
			if(goodsClass.getParentId()==parentId){
				Map<String,Object> dataMap = getGoodsClassData(goodsClass);
				if("0".equals(goodsClass.getIsEndNode())){	//如果不是叶子节点，则查询子节点
					dataMap.put("children", getGoodsCateList(goodsClassList, goodsClass.getId()));
				}else{
					if(parentId==null){
						dataMap.put("children", new ArrayList<Object>());	//如果是第一级分类，加一个空的children
					}
				}
				goodsClassMapList.add(dataMap);
			}
		}
		
		return goodsClassMapList;
	}
	
	/**
	  * <h2>取得商品分类实体对象数据</h2>
	  * @Title: getGoodsCateData
	  * @Description: 取得商品分类实体对象数据
	  * @param @param goodsCate
	  * @return Map<String,Object>    返回类型
	 */
	private static Map<String,Object> getGoodsClassData(GoodsClass goodsClass){
		Map<String,Object> dataMap = new LinkedHashMap<String,Object>();
		dataMap.put("id",goodsClass.getId());
		dataMap.put("code",goodsClass.getCode());
		dataMap.put("name",goodsClass.getName());
		dataMap.put("parentId",goodsClass.getParentId());
		dataMap.put("isEndNode",goodsClass.getIsEndNode());
		dataMap.put("depth",goodsClass.getDepth());
		dataMap.put("icon",goodsClass.getIcon());
		
		return dataMap;
	}
}
